#ifndef _VFX_SOFTBODY_H_
#define _VFX_SOFTBODY_H_

#include "ui/ui_sprite.h"

namespace vfx
{

class	SFXSoftbody
{
public:
	struct				MassPart
	{
		math::Vec2		LastPos, Pos, Speed;
		float			Mass;
	};
	struct				Spring
	{
		Spring() : pA(NULL), pB(NULL) {};
		MassPart*		pA;
		MassPart*		pB;
		float			Length, DesiredLength, Force;
	};
	enum				SPRING_CONFIG
	{
		SPRING_GRID			= 0x01 << 0,
		SPRING_XBRACE		= 0x01 << 1,
		SPRING_FLEXION		= 0x01 << 2,
		SPRING_SUPER		= 0x01 << 3,
		// some basic 
		SPRING_RIGID		= SPRING_GRID | SPRING_XBRACE | SPRING_FLEXION | SPRING_SUPER,
		SPRING_PAPER		= SPRING_GRID | SPRING_XBRACE| SPRING_FLEXION,
		SPRING_CLOTH		= SPRING_GRID | SPRING_XBRACE,
	};

						SFXSoftbody();
						~SFXSoftbody();
	virtual void		Init(const std::string& Image, int Width, int Height, unsigned int	Config, float Mass, float SpringStrength);
	virtual void		Tick(float dt);
	virtual void		Render(const math::Vec2& Offset, float Scale);
	virtual void		RenderDebug(const math::Vec2& Offset, float Scale);
	MassPart*			GetMass(int x, int y);
	MassPart*			AddMass(const math::Vec2& P1);
	Spring*				AddSpring(MassPart* pA, MassPart* pB, float Spring = -1);
	void				SetPos(const math::Vec2& ScreenPos) { m_ScreenPos = ScreenPos; };
	void				UpdateAllSprings(float Strength);
	math::Vec2			GetGridPos(int x, int y);



	void				SetIsFlipped(bool b) { m_IsFlipped = b; };
	void				SetGravity(float f) { m_DefaultGravity = f; };
	const math::Vec2&	GetSize() const { return m_Size; };
	int					GetWidth() const { return m_Width; };
	int					GetHeight() const { return m_Height; };
	void				SetColor(COLOR c) { m_Color = c; };
protected:
	void				Clear();
	void				RenderGrid(const math::Vec2& Offset, float Scale);
	void				RenderRope(const math::Vec2& Offset, float Scale);


	std::vector<MassPart*>	m_Parts;
	std::vector<Spring*>	m_Springs;
	float					m_DefaultSpring, m_DefaultMass, m_DefaultGravity;
	int						m_Width, m_Height;
	MassPart**				m_NodeA;
	ui::UISprite			m_Image;
	math::Vec2				m_Size, m_ScreenPos;
	COLOR					m_Color;
	bool					m_IsFlipped;

};

}

#endif //_VFX_SOFTBODY_H_